var express = require('express');
var router = express.Router();
var request = require('request');
var qs = require('qs');
var path = require('path');

router.get('/',function(req,res) { //业务主页
    if(req.session.token) {
        res.sendFile(path.join(__dirname,"../src/templates/business.html"));
    }else {
        res.redirect("/");
    }
});

router.get('/deposit',function(req,res) { //存款主页
    if(req.session.token) {
        res.sendFile(path.join(__dirname,"../src/templates/deposit.html"));
    }else {
        res.redirect("/");
    }
});

router.post('/deposit',function(req,res) { //存款金额
    if(req.session.token) {
        var url = "http://39.106.9.239/ATM/business/deposit";
        var formData = {
            "amount" : req.body.Money,
        }
        request({
            url: url,
            method: "post",
            headers: {
                "content-type" : "application/x-www-form-urlencoded",
                "authorization" : req.session.token,
                "deviceNo" : 100
            },
            body: qs.stringify(formData)
        },function(_err,_res,_resBody){
            if(!_err && _res.statusCode == 200) {
                if(JSON.parse(_resBody).code == 0) {
                    req.session.count = JSON.parse(_resBody).data.count;
                    req.session.amount = JSON.parse(_resBody).data.amount;
                    req.session.adviceId = JSON.parse(_resBody).data.adviceId;
                }
                res.send(_resBody);
             }else {
                 console.log(_err);
             }
         });
    }else {
        res.redirect("/");
    }
});

router.get('/depositSuccess',function(req,res) { //存款成功
    if(req.session.token) {
        res.sendFile(path.join(__dirname,"../src/templates/depositSuccess.html"));
    }else {
        res.redirect("/");
    }
});
router.post('/deposit/depositsuccess',function(req,res) { //存款成功数据初始化
    if(req.session.token) {
        var data = {
            "data" : {
                "count" : req.session.count,
                "amount" : req.session.amount
            }
        }
        res.send(JSON.stringify(data));
     }else {
         res.redirect("/");
     }
});
module.exports = router;